﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using IQ.DbBase;
using IQ.BaseWindows;
using SqlServerAdmin;
using System.Data.Common;
using System.Data.SqlClient;

namespace IQ.DbA
{
    public partial class FrmServerDir : Form
    {
		#region # Constructor # 
        public FrmServerDir()
        {
            InitializeComponent();
            cbxDrives.SelectedIndex = 0;
        }
		#endregion
		
		#region - Private variables - 
        private DataTable dtServer;
        private string curDrv = "";
        private string curPath = "";
        private string selectedFile;
		#endregion 

		#region - Private methods - 
        private void addAvailableDrives()
        {
            int FreeMeg = 0;
            this.Cursor = Cursors.WaitCursor;
            Application.DoEvents();
            if (dtServer == null)
                dtServer = DbHelper.Load_Table(SqlQueries.SqlServerDrives);
            Application.DoEvents();
            tvServerDir.Nodes.Clear();
            cbxDrives.Items.Clear();
            foreach (DataRow dr in dtServer.Rows)
            {
                if (Convert.ToInt32(dr["media type"]) == 2)
                {
                    FreeMeg = (int)Math.Round(Convert.ToDouble(dr["low free"]) / 1024 / 1024);
                    cbxDrives.Items.Add(dr["name"].ToString() + " (" + FreeMeg + "MB free)");
                }
            }
            this.Cursor = Cursors.Default;
            cbxDrives.Text = "please select";
        }
        private void buildTree(TreeNode ParentN)
        {
            this.Cursor = Cursors.WaitCursor;
            Application.DoEvents();
            DataTable dtDir = DbHelper.Load_Table(SqlQueries.GetSqlServerDirectory(curPath));
            Application.DoEvents();
            foreach (DataRow dr in dtDir.Rows)
            {
                TreeNode tn;// = new TreeNode();
                if (ParentN == null)
                    tn = tvServerDir.Nodes.Add(dr["subdirectory"].ToString());
                else
                    tn = ParentN.Nodes.Add(dr["subdirectory"].ToString());
                //tn.SelectedIndex = 2;
                if (Convert.ToInt32(dr["file"]) == 1)
                    tn.ImageIndex = 1;
                else
                    tn.Nodes.Add(" ");
            }
            this.Cursor = Cursors.Default;
        }
        private string getFullPath(TreeNode tn)
        {
            string result = tn.Text;
            TreeNode tnParent = tn.Parent;
            while (tnParent != null)
            {
                result = tnParent.Text + "\\" + result;
                tnParent = tnParent.Parent;
            }
            return curDrv + result;
        }
		#endregion 
		
        #region - Public properties / variables -
        public string SelectedFile
        {
            get { return selectedFile; }           
        }
        #endregion

		//#region - Public methods - 
		//#endregion
		
		#region - Bind Methods - 
		#endregion
		
		#region - Form Events -
		// This region is for event handler methods that are called without the user knowing about it.
		// Such as the page loading.
        private void FrmServerDir_Shown(object sender, EventArgs e)
        {
            selectedFile = "";
            addAvailableDrives();
        }

        private void tvServerDir_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            if (e.Node.Level == 0)
                curPath = curDrv + e.Node.Text;
            else
                curPath = getFullPath(e.Node);
            //
            lblCurPath.Text = curPath;
            //
            if (e.Node.Nodes.Count == 1)
            {
                e.Node.Nodes.Clear();// DeleteChildren();
                buildTree(e.Node);
            }
        }

		#endregion 
		
		#region - Control Methods -
		// This region is for event handler methods that are called knowingly by the user.
		// Such as a button being clicked.
        private void cbxDrives_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbxDrives.Text.Trim() != "")
            {
                tvServerDir.Nodes.Clear();
                curDrv = cbxDrives.Text.Substring(0, cbxDrives.Text.IndexOf(' ')).Trim();
                curPath = curDrv;
                buildTree(null);
                //btnSel.Enabled := True;
            }
        }
        private void btnSelect_Click(object sender, EventArgs e)
        {
            if (tvServerDir.SelectedNode != null)
            {
                TreeNode tn = tvServerDir.SelectedNode;
                //
                if (tn != null)
                    lblFile.Text = curPath + "\\" + tvServerDir.SelectedNode.Text;
                else
                    lblFile.Text = curPath; //+tvDir.Selected.Text;
                //
                selectedFile = lblFile.Text;
                this.Close();
            }
            else
                General.MesDlg("No item selected.", 'E');
        }
        #endregion

    }
}
